<?php
namespace App\Services\Presenters;
use App\Services\PermissionsService\PermissionsService;
use App\Permission;
use Illuminate\Support\Facades\Cache;
use Route;
use Auth;
use DB;
use Log;

class MenuPresenter
{
	/**
     * 权限仓库实现.
     *
     * @var PermissionsService
     */
    protected $menu;

    /**
     * 创建一个新的属性composer.
     *
     * @param PermissionsService $menu
     * @return void
     */
    public function __construct(PermissionsService $menu)
    {
        $this->menu = $menu;
    }

	//加上缓存
	public function cacheMenu(){
//		$html = Cache::remember('sidebarMenus', 120, function() {
//			return $this->sidebarMenus();
//		});
//		return $html;
		return $this->sidebarMenus();
	}

	/**
	 *  [sidebarMenus 左侧菜单渲染]
	 *  izxin.com
	 *  @author chouchong
	 *  @DateTime 2016-09-18T17:26:26+0800
	 *  @param    [type]                   $menus [description]
	 *  @return   [type]                          [description]
	 */
	public function sidebarMenus($menus = array())
	{
		$menus = $this->menu->getPermissionParent();
		$rule = $this->menu->getMenuId(Route::currentRouteName());
		//rule为空 有可能是还未加入到数据库中
//		if(empty($rule)){
//			$route = Route::current();
//			//dd($route->getAction()['prefix']);
//			$result = preg_replace('/\/(backstage){1}\/*/i', '',$route->getAction()['prefix']);
//			$rule = $this->menu->getMenuId($result);
//			dd($rule);
//		}
		$parent = array();
		$child = array();
		if(!empty($rule)){
			if($rule['zMenu']['parent_id'] > 0){
				$parent = $rule['fMenu'];
			}
			$child = $rule['zMenu'];
		}
		$html = '';
		if ($menus) {
			foreach ($menus as $v) {
				if (Auth::user()->is_super_administrator === 1 || Auth::user()->can($v['name'])) {
					if(!empty($child) && !empty($parent)){
						if($v['id'] == $child['parent_id'] || (isset($parent['parent_id']) && $parent['parent_id'] == $v['id'])){
							$active = 'active';
							$open = 'open';
						}else{
							$active = '';
							$open = '';
						}
					}else{
						$active = '';
						$open = '';
					}
					$html .= '<li class="'.$open.'">';
					//子  $html .= '<i class="'.$v['icon'].'"></i>';
					if ($v['child']) {
						$html .= '<a href="javascript:;" class="dropdown-toggle '.$active.'">';
						    $html .= '<i class="icon-desktop"></i>';
						    $html .= '<span class="menu-text">'.$v['display_name'].'</span>';
						    $html .= '<b class="arrow icon-angle-down"></b>';
						$html .= '</a>';
						$html .= $this->getSidebarChildMenu($v['child'],$child,$parent,$active);
					}
					$html .= '</li>';
				}
			}
		}
		return $html;
	}
	/**
	 *  [getSidebarChildMenu 左侧菜单子菜单渲染]
	 *  izxin.com
	 *  @author chouchong
	 *  @DateTime 2016-09-18T17:26:16+0800
	 *  @param    string                   $childMenu [description]
	 *  @return   [type]                              [description]
	 */
	public function getSidebarChildMenu($childMenu='',$child,$parent = array(),$f_active = '')
	{
		//只有二级权限
		$html = '';
		$active = '';
		if ($childMenu) {
			$show = $f_active == 'active' ? 'style="display: block;"' : '';
			$html .= '<ul class="submenu" '.$show.'>';
			foreach ($childMenu as $v) {
				if (Auth::user()->is_super_administrator === 1 || Auth::user()->can($v['name'])) {
					try{
						$url = route($v['name']);
					}catch(\Exception $e){
						Log::info('生成右侧导航栏出错');
						Log::info($e);
						continue;
					}
					if(!empty($child) && !empty($parent)){
						if($v['id'] == $child['id'] || $child['parent_id'] == $v['id']){
							$active = 'active';
						}else{
							$active = '';
						}
					}else{
						$active = '';
					}
					$html .= '<li class="'.$active.'"><a href="'.$url.'"><i class="icon-double-angle-right"></i>'.$v['display_name'].'</a></li>';
				}
			}
			$html .= '</ul>';
		}
		return $html;
	}
}